<template>
  <div class="articles-container">
    <el-card>
      <!-- 搜索区域 -->
      <el-row>
        <el-col :span="18">
          <el-form ref="formRef" label-width="80px" inline size="small">
            <el-form-item label="关键字">
              <el-input
                v-model="queryInfo.keyword"
                placeholder="根据文章标题搜索"
              ></el-input>
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="queryInfo.state" placeholder="请选择">
                <el-option label="启用" :value="1"></el-option>
                <el-option label="禁用" :value="0"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button size="small" @click="clean">清除</el-button>
              <el-button type="primary" size="small" @click="search"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="6" style="text-align: right">
          <el-button
            type="success"
            icon="el-icon-edit"
            size="small"
            @click="openAddDialog"
            >新增技巧</el-button
          >
        </el-col>
      </el-row>

      <!-- 提示框组件 -->
      <el-alert
        :title="`数据一共 ${total} 条`"
        type="info"
        show-icon
        :closable="false"
        style="margin-bottom: 15px"
      >
      </el-alert>

      <!-- table组件 -->
      <el-table :data="articlesList" style="width: 100%">
        <el-table-column
          type="index"
          label="序号"
          width="80px"
        ></el-table-column>
        <el-table-column label="文章标题" width="400px">
          <template #default="{ row }">
            {{ row.title }}
            <a
              href="#"
              @click="openVideo(row.videoURL)"
              v-if="row.videoURL"
              class="el-icon-film"
              style="color: #09f"
            ></a>
          </template>
        </el-table-column>
        <el-table-column
          prop="visits"
          label="阅读数"
          width="width"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="录入人"
          width="width"
        ></el-table-column>
        <el-table-column label="录入时间" width="155px">
          <template #default="{ row }">
            {{ formatTime(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100px">
          <template #default="{ row }">
            {{ row.state === 0 ? "已禁用" : "已启用" }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180px">
          <template #default="{ row }">
            <el-button type="text" @click="previewArticle(row)">预览</el-button>
            <el-button type="text" @click="toggleState(row)">{{
              row.state === 0 ? "启用" : "禁用"
            }}</el-button>
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="openEditDialog(row)"
              >修改</el-button
            >
            <el-button
              type="text"
              :disabled="row.state === 1"
              @click="delArticle(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 20, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="text-align: center"
      >
      </el-pagination>
    </el-card>

    <!-- 新增技巧对话框子组件 -->
    <ArticlesAdd :showDialog.sync="showDialog" ref="dialog"></ArticlesAdd>

    <!-- 文章预览对话框子组件 -->
    <ArticlesPreview
      :showPreviewDialog.sync="showPreviewDialog"
      :currentArticle="currentArticle"
      ref="preview"
    ></ArticlesPreview>
  </div>
</template>

<script>
// 导入获取文章列表,切换文章状态的方法
import { list as getArticlesListApi, changeState as changeStateApi, remove as delArticleApi } from '@/api/hmmm/articles.js'
// 导入dayjs
import dayjs from 'dayjs'
// 导入新增文章子组件
import ArticlesAdd from '../components/articles-add.vue'
// 导入文章预览子组件
import ArticlesPreview from '../components/articles-preview.vue'
export default {
  components: {
    ArticlesAdd,
    ArticlesPreview
  },
  created () {
    this.getArticlesList()
  },
  data () {
    return {
      // 查询参数
      queryInfo: {
        page: 1,
        pagesize: 10,
        keyword: '',
        state: null
      },
      // 分页组件的total
      total: 0,
      // 文章列表
      articlesList: [],
      // 控制对话框的显示与隐藏
      showDialog: false,
      // 控制预览对话框的现实与隐藏
      showPreviewDialog: false,
      // 当前操作的文章
      currentArticle: {}
    }
  },
  methods: {
    // 获取文章列表
    async getArticlesList () {
      const { data } = await getArticlesListApi(this.queryInfo)
      console.log(data)
      this.articlesList = data.items
      this.total = data.counts
    },
    // 监听pagesize的改变
    handleSizeChange (newPagesize) {
      console.log(newPagesize)
    },
    // 监听page的改变
    handleCurrentChange (newPage) {
      console.log(newPage)
    },
    // 搜索
    search () {
      this.getArticlesList(this.queryInfo)
    },
    // 清除
    clean () {
      this.queryInfo.keyword = ''
      this.queryInfo.state = null
      this.getArticlesList()
    },
    // 格式化时间
    formatTime (time, str = 'YYYY-MM-DD HH:mm:ss') {
      return dayjs(time).format(str)
    },
    // 切换状态
    async toggleState (row) {
      const data = await changeStateApi({ id: row.id, state: row.state === 0 ? '1' : '0' })
      console.log(data)
      row.state = (row.state === 0 ? '1' : '0')
      this.getArticlesList()
    },
    // 打开视频
    openVideo (videoURL) {
      console.log(videoURL)
    },
    // 打开添加对话框
    openAddDialog () {
      this.showDialog = true
    },
    // 打开编辑对话框
    openEditDialog (row) {
      this.showDialog = true
      this.$refs.dialog.getArticleDetail(row.id)
    },
    // 删除文章
    async delArticle (row) {
      console.log(row)
      try {
        await this.$confirm(`确定删除 ${row.title} 文章吗?`)
        await delArticleApi({ id: row.id })
        this.$message.success('删除成功')
        this.getArticlesList()
      } catch (error) {
        console.log(error)
        this.$message.info('取消了删除')
      }
    },
    // 打开预览对话框
    previewArticle (row) {
      console.log('row', row)
      this.currentArticle = row // 不调用子组件获取文章详情的方法,是因为获取文章详情返回的数据比row少了一个username
      this.showPreviewDialog = true
    }
  }
}
</script>

<style scoped lang="scss">
.articles-container {
  padding: 10px 10px;
}
</style>
